﻿
var elId                = $('#userid'),
    elPassword          = $('#password'),
    elRepeatPassword    = $('#repeatPassword'),
    elPhone             = $('#phone'),
    elError             = undefined,
    xhr                 = undefined;

elId.on('focusout',function () {
    elError = elId.next().next();
    checkId();
});
elPassword.on('focusout', function (){
    elError = elPassword.next().next();
    checkPassword();
});
elRepeatPassword.on('focusout', function (){
    elError = elRepeatPassword.next().next();
    checkRepeatPassword();
});
elPhone.on('focusout', function (){
    elError = elPhone.next().next();
    checkPhone();
});

function checkId(){
    var id = elId.val().trim();
    if(elId.val() !== id){
        elError.text('用户名不能包含空格');
        return;
    }
    if(id === null || id === '' || id.length === 0){
        elError.text('用户名不得为空');
        return;
    }
    elError.text('');
    $.ajax({
        type    : 'GET',
        url     : 'CheckUserid?type=register&userid=' + id,
        success : function (data){
            if(data === 'Exist'){
                elError.text('用户名已存在');
            }
        },
        error   : function (errorMsg){
            console.log(errorMsg);
        }
    });
}

function checkPassword(){
    var password = elPassword.val();
    if(password === null || password === '' || password.length === 0){
        elError.text('密码不能为空');
        return;
    }
    elError.text('');
}

function checkRepeatPassword(){
    if(elPassword.val() !== elRepeatPassword.val()){
        elError.text('两次输入密码不一致');
        return;
    }
    elError.text('');
}

function checkPhone(){
    var phone = elPhone.val().trim();
    var reg = /[^012345789-]/
    if(phone !== elPhone.val()){
        elError.text('手机号不能包含空格');
        return;
    }
    if(phone === null || phone === '' || phone.length === 0){
        elError.text('手机号不能为空');
        return;
    }
    if(reg.test(phone)){
        elError.text('手机号必须为纯数字');
        return;
    }
    elError.text('');
}

function reloadCaptcha(){
    var url = "Captcha?key=" + new Date().getMilliseconds();
    $("#captcha").attr("src", url);
}